package fy.mdm.dao;

import com.soa.eis.adapter.framework.message.IMsgObject;
import com.soa.eis.adapter.framework.message.impl.GroupRecord;
import com.soa.eis.adapter.framework.message.impl.MsgObject;
import com.soa.eis.adapter.framework.requester.IServiceRequester;
import com.soa.eis.adapter.framework.requester.impl.BaseServiceRequester;
import fy.config.FyConfig;
import fy.mdm.bean.PartEditCustomerBean;
import fy.mdm.bean.PartNewCustomerBean;
import fy.util.DBUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class PartEditCustomerDao {
    private PartEditCustomerBean partEditCustomerBean;
    private static Log log = LogFactory.getLog(FiexdAssetReviewDao.class);
    public boolean StateReturn() throws Exception{
        Connection conn = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        IServiceRequester Requester = BaseServiceRequester.getInstance();
        List<GroupRecord> grs1 = new ArrayList<GroupRecord>();//多层用
        // 定义请求报文对象
        IMsgObject reqMo = new MsgObject(IMsgObject.MOType.initSR);
        String SourceSysID = "06004"; // 请求方(发布方)
        String ServiceID = "06001000000043";// 响应方(发布方)
        // 获取系统时间
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String format = simpleDateFormat.format(date);
        // 对请求报文对象进行设置
        reqMo.setSourceSysID(SourceSysID); // 设置报文 请求方系统号
        reqMo.setSerialNO(format.substring(0, 8) + SourceSysID
                + String.format("%06d", ++MaterialRequestNoTimerTask.requestNo));// 设置报文流水号,第三段表示今天发送的第几的一次报文
        reqMo.setServiceID(ServiceID); // 设置报文 提供方服务号
        reqMo.setServiceDateTime(format); // 设置报文 日期时间
        reqMo.setReqValue("ControlType","5" );
        reqMo.setReqValue("BOID","SOA_MDM_CUSTOMER_OTHER_F" );

        GroupRecord gr1 = new GroupRecord();
//        FyConfig.setOA_DB_CONFIG("v3xuser", "fyoa2009",
//                "jdbc:oracle:thin:@10.112.17.151:1521:oadb", "oadb",
//                "oracle.jdbc.driver.OracleDriver");
        log.info("setOA_DB_CONFIG---->>>");
        conn = DBUtil.connect(FyConfig.getOA_DB_CONFIG());
        log.info("connect---->>>");
        conn.setAutoCommit(false);

        //pst = conn.prepareStatement("select L.CODE,H.CREATE_DATE from formmain_27560 T LEFT JOIN ORG_MEMBER L on T.MODIFY_MEMBER_ID = L.ID LEFT JOIN COL_SUMMARY H ON T.ID = H.FORM_RECORDID  where H.ID = ?");
        pst = conn.prepareStatement("select L.CODE,H.CREATE_DATE from formmain_21376 T LEFT JOIN ORG_MEMBER L on T.MODIFY_MEMBER_ID = L.ID LEFT JOIN COL_SUMMARY H ON T.ID = H.FORM_RECORDID  where H.ID = ?");//测试
        pst.setLong(1,this.partEditCustomerBean.getSummaryId());
        log.info("createStatement---->>>"+this.partEditCustomerBean.getSummaryId());

        rs = pst.executeQuery();

        log.info("executeQuery---->>>");
        while (rs.next()) {
            gr1.setFieldValue("APPROVER",rs.getString(1));
            gr1.setFieldValue("CREATE_TIME",rs.getString(2));
            log.info(rs.getString(1));
        }
        pst = conn.prepareStatement("select  l.CODE from ORG_MEMBER l where l.ID = ?");
        pst.setLong(1,this.partEditCustomerBean.getApplier());
        rs = pst.executeQuery();
        log.info("getApplier ----->>"+this.partEditCustomerBean.getApplier());
        while (rs.next()) {
            gr1.setFieldValue("CREATOR",rs.getString(1));
            log.info("CREATOR = "+rs.getString(1));
        }
        DBUtil.close(rs);
        gr1.setName("ROW");

        gr1.setFieldValue("CUSTOMER_CODE",this.partEditCustomerBean.getCUSTOMER_CODE());
        gr1.setFieldValue("CUSTOMER_NAME",this.partEditCustomerBean.getCUSTOMER_NAME());
        gr1.setFieldValue("CUSTOMER_STYLE",getEnumValue("客户类型-客户变更审批表（配件）",this.partEditCustomerBean.getCUSTOMER_STYLE()));
        gr1.setFieldValue("COUNTRY",getEnumValue("客户国别",this.partEditCustomerBean.getCOUNTRY()));
        gr1.setFieldValue("CUSTOMER_SHORT_NAME",this.partEditCustomerBean.getCUSTOMER_SHORT_NAME());
        log.info("DAYS="+this.partEditCustomerBean.getDAYS());
        gr1.setFieldValue("DAYS",getEnumValue("信用天数",this.partEditCustomerBean.getDAYS()));
        gr1.setFieldValue("AREA",this.partEditCustomerBean.getAREA());
        gr1.setFieldValue("TAX_REGISTRATION_NUMBER",this.partEditCustomerBean.getTAX_REGISTRATION_NUMBER());
        gr1.setFieldValue("ADDRESS",this.partEditCustomerBean.getADDRESS());
        gr1.setFieldValue("CUSTOMER_CATEGORY",this.partEditCustomerBean.getCUSTOMER_CATEGORY());
        gr1.setFieldValue("PAYMENT_CLAUSE",this.partEditCustomerBean.getPAYMENT_CLAUSE());
        gr1.setFieldValue("PRICE_LIST",this.partEditCustomerBean.getPRICE_LIST());
        gr1.setFieldValue("LEGAL_REPRESENTATIVE",this.partEditCustomerBean.getLEGAL_REPRESENTATIVE());
        gr1.setFieldValue("LEGAL_REPRESENTATIVE_PHONE",this.partEditCustomerBean.getLEGAL_REPRESENTATIVE_PHONE());
        gr1.setFieldValue("NAME",this.partEditCustomerBean.getNAME());
        gr1.setFieldValue("PHONE",this.partEditCustomerBean.getPHONE());
        gr1.setFieldValue("PROVINCE",this.partEditCustomerBean.getPROVINCE());
        gr1.setFieldValue("CITY",this.partEditCustomerBean.getCITY());
        gr1.setFieldValue("ADDRESS1",this.partEditCustomerBean.getADDRESS1());
        log.info("ADDRESS1="+this.partEditCustomerBean.getADDRESS1());
        gr1.setFieldValue("ADDRESS2",this.partEditCustomerBean.getADDRESS2());
        gr1.setFieldValue("ADDRESS3",this.partEditCustomerBean.getADDRESS3());
        gr1.setFieldValue("APPROVER_TIME",this.partEditCustomerBean.getAPPROVER_TIME());
        String Status = "";
        if(getEnumValue("客户状态-客户变更（配件）",this.partEditCustomerBean.getSTATUS()).equals("有效(Active)")){
            Status = "Active";
        }else if(getEnumValue("客户状态-客户变更（配件）",this.partEditCustomerBean.getSTATUS()).equals("失效(Inactive)")){
            Status = "Inactive";
        }
        gr1.setFieldValue("STATUS",Status);
        gr1.setFieldValue("TRANSACTION_TYPE","2");
        //gr1.setFieldValue("CREATOR",this.partNewCustomerBean.getApplier());
        grs1.add(gr1);
        reqMo.setReqGroupRecord(grs1);

        log.info("Applier = "+this.partEditCustomerBean.getApplier());
        log.info("SummaryId = "+this.partEditCustomerBean.getSummaryId());
        log.info("reqMo:"+reqMo);
        System.out.println(reqMo);
        IMsgObject respMo = null;
        respMo = Requester.execute(reqMo);//发布报文
        return true;
    }
    public String getEnumValue(String enumName,String value)throws Exception{
        log.info("value = "+value);
        Connection conn = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        conn = DBUtil.connect(FyConfig.getOA_DB_CONFIG());
        conn.setAutoCommit(false);
        pst = conn.prepareStatement("select l.SHOWVALUE from CTP_ENUM t left join CTP_ENUM_ITEM l on t.ID = l.REF_ENUMID WHERE l.ENUMVALUE = ? AND t.ENUMNAME = ?");
        pst.setString(1,value);
        pst.setString(2,enumName);
        rs = pst.executeQuery();
        while (rs.next()) {
            log.info("getEnumValue = "+rs.getString(1));
            return rs.getString(1);
        }
        DBUtil.close(rs);
        return "";
    }
    public void setPartEditCustomerBean(PartEditCustomerBean partEditCustomerBean) {
        this.partEditCustomerBean = partEditCustomerBean;
    }
}
